import axios from 'axios'
import md5 from 'js-md5';
import CryptoJS from 'crypto-js'
import router from '../router/index'
import { showNotify, closeNotify } from 'vant';

// create an axios instance
const service = axios.create({
//   baseURL: "/dev-api",
 // url = base url + request url
    // 基地址
    baseURL:'https://apidispatch.jxbly.com/',
  // withCredentials: true, // send cookies when cross-domain requests
    timeout: 20000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    config.headers['Token'] = localStorage.getItem('token')
    config.headers['sign'] = get_sign(localStorage.getItem('token'));
    return config
  },
  error => {
    // do something with request error
    // console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    if (response.data.code === 200) {
        // token过期，清空token,重新登录
        localStorage.clear() //清空本地token,和权限信息
        sessionStorage.clear()
        showNotify(response.data.msg);
        router.push('/login')
    }
    if (response.status === 200) {
      return Promise.resolve(response)
    } else {
      return Promise.reject(response)
      }
  },
  error => {
    // console.log('err' + error) // for debug
    console.log(error)
    showNotify('系统超时，请稍后再试……');
    return Promise.reject('系统超时，请稍后再试……')
  }
)
// function Thistime1() { //当前时间
//   var date = new Date();
//   var year = date.getFullYear();
//   var month = date.getMonth() + 1;
//   var day = date.getDate();
//   var mytime=date.toLocaleTimeString();     //获取当前时间
//   // myDate.toLocaleString( );
//   var time = year + "-" + month + "-" + day+' '+mytime;
//   return time;
// }
function Thistime() { //当前日期 
  var date = new Date();
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var day = date.getDate();
  var hours = date.getHours();
  var Minutes = date.getMinutes();
  var Seconds = date.getSeconds();
 if(month<10){
  month="0"+month;
 }
 if(day<10){
  day="0"+day;
 }
 if(hours<10){
  hours="0"+hours;
 }
 if(Minutes<10){
  Minutes="0"+Minutes;
 }
 if(Seconds<10){
  Seconds="0"+Seconds;
 }
  // myDate.toLocaleString( );
  var time = year + "" + month + "" + day+''+hours+''+Minutes+''+Seconds;
  return time;
}

//  AES加密
 function encryptAES(word, keyStr) {  
  // console.log(word);  
  // console.log(keyStr);
   var key = CryptoJS.enc.Utf8.parse(keyStr.substring(0,16));
   var viStr = CryptoJS.enc.Utf8.parse(keyStr.substring(0,16));//偏移量
   let srcs = CryptoJS.enc.Utf8.parse(word);
   let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: viStr, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
   var res = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
    // console.log('加密结果： res=',res);
   return res;
}



function get_sign(token)
{
  if(token!=null)
  {
    if(token.length>16)
  {
  // console.log(token+Thisday())
    var sign=md5(md5(token+Thistime()));//md5加密 [两次加密]
    sign=sign+encryptAES(token+"_"+Thistime(),token);//拼接AES加密
    return sign;
  }
}
 
    return "";
//  console.log(sign);

}
export default service
